from pixel import *
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

class Command:
    def __init__(self):
        self.undone = False

    def do(self):
        """ implement me """
    def undo(self):
        """ implement me """

class PaintingCommand(Command):
    def __init__(self, pixmap, layer = None):
        Command.__init__(self)
        self.target_pixmap = pixmap
        self.layer = layer
        self.old_pixmap = QPixmap(self.target_pixmap.size())
        self.old_pixmap.fill(QColor(0,0,0,0))
        p = QPainter(self.old_pixmap)
        p.drawPixmap(0,0,self.target_pixmap)
        p.end()

    def undo(self):
        self.undone = True
        self.target_pixmap.fill(QColor(0,0,0,0))
        p = QPainter(self.target_pixmap)
        p.drawPixmap(0,0,self.old_pixmap)
        p.end()

class HSVOffsetCommand(PaintingCommand):
    """ Paint Command """
    def __init__(self, pixmap, h, s, v):
        PaintingCommand.__init__(self, pixmap)
        self.h = h 
        self.s = s
        self.v = v

    def __str__(self):
        return "HSVOffset command!"

    def do(self):
        self.undone = False

        image = self.target_pixmap.toImage()
        image = Pixel.hsv_offset(image, self.h, self.s, self.v)
        
        p = QPainter(self.target_pixmap)
        p.drawImage(0, 0, image)
        p.end()

class FillCommand(PaintingCommand):
    """ Paint Command """
    def __init__(self, pixmap, x, y, color):
        PaintingCommand.__init__(self, pixmap)
        self.x = x
        self.y = y
        self.color = color

    def __str__(self):
        return "Fill command!"

    def do(self):
        self.undone = False
        image = self.target_pixmap.toImage()
        pixel = Pixel.getPixel(image, self.x, self.y)
        
        fill_color = self.color

        # comparing QColor objects with == not worky
        if not (pixel.color.red() == fill_color.red() and pixel.color.blue() == fill_color.blue() and pixel.color.green() == fill_color.green() and pixel.color.alpha() == fill_color.alpha()):
            Pixel.flood_fill(image, self.x, self.y, pixel.color, fill_color)

        p = QPainter(self.target_pixmap)
        p.drawImage(0, 0, image)
        p.end()


class PaintCommand(PaintingCommand):
    """ Paint Command """
    def __init__(self, pixmap, x, y, lx, ly, pen, erasing = False, multiplier = 1, layer = None):
        PaintingCommand.__init__(self, pixmap, layer)
        self.x = x
        self.y = y
        self.lx = lx
        self.ly = ly
        self.pen = pen
        self.erasing = erasing
        self.multiplier = multiplier

    def __str__(self):
        return "Pencil command!"
        
    def do(self):
        self.undone = False
        p = QPainter(self.target_pixmap)
        p.setPen(self.pen)
        if self.erasing:
            p.setCompositionMode(QPainter.CompositionMode_Clear)
            p.eraseRect(self.x, self.y, 1, 1)
        else:
            p1 = QPoint(self.x, self.y)
            p2 = QPoint(self.lx, self.ly)
            p.setCompositionMode(QPainter.CompositionMode_SourceOver)
            p.drawLine(p1, p2)
        p.end()

    def freeze_layer(self, layer):
        self.old_layer = layer.copy(layer.rect())

    def undo(self):
        self.undone = True
                
        self.layer.fill(QColor(0,0,0,0))
        q = QPainter(self.layer)
        q.drawPixmap(0,0,self.old_layer)
